#!/usr/bin/perl
# -*- mode: Perl; tab-width: 4; -*-


#Make the a root-volume FINK_ROOT symlink to the FINK_ROOT on the target volume if
#user selected non-root volume (InstallationCheck verified that there
#was no existing FINK_ROOT on root volume)
if(! -e "FINK_ROOT") {
	`/bin/ln -s \"$ARGV[1]FINK_ROOT\" FINK_ROOT`;
}

# final preparations to create a viable fink installation
system "/usr/bin/yes '' | FINK_ROOT/lib/fink/postinstall.pl";
system "FINK_ROOT/bin/fink -y index";

# get ps data for a given PID
sub process_info {
        my($pid) = shift;
        my $command = "ps -xww -p$pid -o user,ppid,tty,command | tail -n 1";
        my @info = split(/\s+/, `$command`, 4);
        return (
                user => $info[0],
                parent => $info[1],
                tty => $info[2],
                command => $info[3]
        );
}

# Find the user who launched the parent of this script.
my %info = process_info($$);          # this script
%info = process_info($info{parent});  # its parent process
while($info{command} =~ /Install\.framework/) {
	# The installer may launch us via some intermediate tools.
	%info = process_info($info{parent});
}

# set up user's shell for fink
# NB: pathsetup.sh requires an Aqua session
if(!$ENV{COMMAND_LINE_INSTALL}) {
	# Running from Installer.app: have Aqua session, so run pathsetup
	# as user who ran Installer.app.
	my $real_user = $info{user};
	system "sudo -H -u $real_user FINK_ROOT/bin/pathsetup.sh";
	system "osascript -e 'tell application \"Installer\"' -e 'activate' -e 'end tell'";
} else {
	# Running from commandline: not certain to have Aqua session, so
	# don't run pathsetup. Instead print message to terminal about
	# what to do.

	# For a command-line install, installer is already run as root, so
	# get its parent.
	%info = process_info($info{parent});

	open(TTY, ">", "/dev/$info{tty}") or *TTY = *STDOUT;;
	print TTY <<END_MESSAGE;

In order for a user to use fink, either:
	test -r FINK_ROOT/bin/init.sh && . FINK_ROOT/bin/init.sh
or
	test -r FINK_ROOT/bin/init.csh && source FINK_ROOT/bin/init.csh
will need to be placed in that user's shell startup script(s).

A user can run FINK_ROOT/bin/pathsetup.sh to make this change for his/her
default shell, or you can place the command to load the appropriate
init file in /etc/profile or other global shell configuration files
so that all users will get it.

END_MESSAGE
}

# pathsetup has been run (or user has been notified to run it manually)
# so now provide message about running configure/selfupdate
# In theory, this is what Conclusion.rtf does, but the 10.7 installer
# doesn't seem to understand it anymore.
if(!$ENV{COMMAND_LINE_INSTALL}) {
	my $real_user = $info{user};
	
	system "osascript -e 'tell app \"Finder\" to activate' -e 'tell app \"Finder\" to display dialog \"To receive the latest package information files from the Fink distribution, open /Applications/Utilities/Terminal.app and run the following two commands in a terminal window:

fink configure
fink selfupdate

Upon completion, your Fink install will be up to date and ready to use.\" buttons \"OK\" default button 1 with title \"Fink installation successful\"' -e 'tell app \"Installer\" to activate'"

	
} else {
	# Running from commandline: not certain to have Aqua session, so
	# just print message to terminal about what to do.

	# For a command-line install, installer is already run as root, so
	# get its parent.
	%info = process_info($info{parent});

	open(TTY, ">", "/dev/$info{tty}") or *TTY = *STDOUT;;
	print TTY <<END_MESSAGE;

Once you have successfully initialized your Fink install, 
run the following two commands to receive the latest package 
information files from the Fink distribution:

fink configure
fink selfupdate

Upon completion, your Fink install will be up to date and ready to use.

END_MESSAGE
}
